package com.youyun.xiaoyuan.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.youyun.xiaoyuan.R;
import com.youyun.xiaoyuan.base.BaseFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by woodnaonly on 2015/12/7.
 */
public class SchoolNewsFragment extends BaseFragment
{
    private ListView mListView;
    private SimpleAdapter mAdapter;
    private ArrayList<Map<String, Object>> mData = new ArrayList<>();
    private SwipeRefreshLayout mRefreshLayout;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        contentView = inflater.inflate(R.layout.school_news_fragment_layout, null);
        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        initadapter();
        initview();


    }

    private void initadapter()
    {

        for (int i = 0; i < 10; i++)
        {
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("img", R.mipmap.ic_launcher);
            listItem.put("text", "Item " + i);
            mData.add(listItem);
        }
        mAdapter = new SimpleAdapter(getContext(), mData, R.layout.list_item, new String[]{"img", "text"}, new int[]{R.id.img, R.id.text});

    }

    private void initview()
    {
        mListView= (ListView) contentView.findViewById(R.id.school_news_fragment_listview);
        mListView.setAdapter(mAdapter);
        mRefreshLayout = (SwipeRefreshLayout) contentView.findViewById(R.id.swipe_container);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
        {
            @Override
            public void onRefresh()
            {
                new Handler().postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        mRefreshLayout.setRefreshing(false);
                        mAdapter.notifyDataSetChanged();
                    }
                }, 2000);
            }
        });
    }
}
